<!DOCTYPE html>
<html lang="en">
	<head>
		<meta charset="UTF-8">
		<meta name="viewport" content="width=device-width, initial-scale=1.0">
		<meta http-equiv="X-UA-Compatible" content="IE=edge" />
		<title>PayPal支付页面</title>
		<style>
			#returnButton {
				display: inline-block;
				padding: 10px 20px;
				background-color: #007BFF;
				color: white;
				border: none;
				border-radius: 4px;
				cursor: pointer;
			}

			#returnButton:hover {
				background-color: #0056b3;
			}
		</style>
	</head>
	<body>


		<!-- <button id="postMessage">发送消息</button> -->
		<!-- <button id="returnButton">返回</button> -->
		<!--  <div id="total"> </div> -->
		<div id="paypal-button-container"></div>
		<!-- 引入 UniApp 的 WebView 脚本 -->
		<script type="text/javascript" src="https://unpkg.com/@dcloudio/uni-webview-js@0.0.1/index.js"></script>
		<script
			src="https://www.paypal.com/sdk/js?client-id=AT_-CH-Jm_PPkl1ai0buSxl0BIXYnXWpCFm-nFnsu6F7INExyBuQ-HQ5ITptsaeq-vqgb6RCVBs7B_LP&currency=USD">
		</script>
		<!-- Include the PayPal JavaScript SDK -->
	
		<script>
			// 在页面加载时存储 如下 到全局变量
			var total;
			var out_trade_no;
			var return_url;
			var paypal_id = 0;
			document.addEventListener('DOMContentLoaded', function() {
				// console.log('Document fully loaded');
				// console.log(window.location.search);
				var url = window.location.search;
				// 使用 URLSearchParams 构造一个 URLSearchParams 对象，将 URL 解析为其组成部分
				const params = new URLSearchParams(url);
				// 获取特定参数的值
				total = params.get('total');
				out_trade_no = params.get('out_trade_no');
				paypal_id = params.get('paypal_id');

				// var totalElement = document.getElementById('total'); // 获取元素
				// if (totalElement) {
				// 	 totalElement.innerHTML = '支付金额：$'+total; // 设置元素内容
				// }

				// 获取按钮元素
				// var returnButton = document.getElementById('returnButton');
				// returnButton.style.display = 'none';
				// returnButton.addEventListener('click', function() {
				//      window.open(return_url, '_blank');
				// });


			});

			window.msgFromUniapp = function(data) {
				console.log(JSON.stringify(data), '接收数据');
				dataInfo = data // 赋值
			}

			paypal.Buttons({
				createOrder: function(data, actions) {
					// 定义一下要付多少钱 
					return paypal_id;
				},
				onApprove: function(data, actions) {
					console.log(JSON.stringify(data))
					console.log(JSON.stringify(actions))


					uni.postMessage({
						oid: data.orderID
					});


					// #ifdef H5
					window.parent.postMessage({
						oid: data.orderID
					})
					// #endif

					return;

				}
			}).render("#paypal-button-container");
		</script>
	</body>
</html>